<?php

Load::lib('gdata');
Zend_Loader::loadClass('Zend_Gdata_YouTube');

class Youtube {

    private $yt;

    public function Youtube() {
        $this->yt = new Zend_Gdata_YouTube(null, null, "ytapi-FredyRamirez-dedicarvideos-h96c0lje-0", "AI39si4iKWr34BEkCeGpQorOUd63tscPf-OmWKmsufQEvDXfVpD54jaGIcCwhH-C68GFKShchPZsNBOHnAa06xlXJxEiDDObeg");
    }

    public function search($searchTerms, $ini) {
        //$yt = new Zend_Gdata_YouTube(null,null,"ytapi-FredyRamirez-dedicarvideos-h96c0lje-0","AI39si4iKWr34BEkCeGpQorOUd63tscPf-OmWKmsufQEvDXfVpD54jaGIcCwhH-C68GFKShchPZsNBOHnAa06xlXJxEiDDObeg");
        $this->yt->setMajorProtocolVersion(2);
        $query = $this->yt->newVideoQuery();
        $query->setOrderBy('relevance');
        $query->setSafeSearch('none');
        //Load::lib('general');
        $query->setParam('restriction', General::getIp());
        $query->setVideoQuery($searchTerms);
        $query->setStartIndex($ini);
        $query->setMaxResults(10);
        $videoFeed = $this->yt->getVideoFeed($query->getQueryUrl(2));
        return $this->feed($videoFeed);
    }

    public function related() {
        
    }

    public function video($videoId) {
        //$yt = new Zend_Gdata_YouTube(null,null,"ytapi-FredyRamirez-dedicarvideos-h96c0lje-0","AI39si4iKWr34BEkCeGpQorOUd63tscPf-OmWKmsufQEvDXfVpD54jaGIcCwhH-C68GFKShchPZsNBOHnAa06xlXJxEiDDObeg");
        $videoEntry = $this->yt->getVideoEntry($videoId);
        return $this->limpio($videoEntry);
    }

    private function feed($videoFeed) {
        $i = 0;
        $return = "";
        foreach ($videoFeed as $videoEntry) {
            $return[$i] = $this->limpio($videoEntry);
            $i++;
        }
        return $return;
    }

    private function limpio($videoEntry) {
        $return['titulo'] = $videoEntry->getVideoTitle();
        //Load::lib('general');
        $return['tituloUrl'] = General::slugify($videoEntry->getVideoTitle());
        $return['videoId'] = $videoEntry->getVideoId();
        $return['actualizado'] = $videoEntry->getUpdated();
        $return['descricion'] = $videoEntry->getVideoDescription();
        $return['categoria'] = $videoEntry->getVideoCategory();
        $return['tags'] = implode(", ", $videoEntry->getVideoTags());
        $return['url'] = $videoEntry->getVideoWatchPageUrl();
        $return['urlFlash'] = $videoEntry->getFlashPlayerUrl();
        $return['Duracion'] = $videoEntry->getVideoDuration();
        $return['Vista'] = $videoEntry->getVideoViewCount();
        $return['raiting'] = $videoEntry->getVideoRatingInfo();
        $return['Localizacion'] = $videoEntry->getVideoGeoLocation();
        $return['recorded'] = $videoEntry->getVideoRecorded();
        $return['incrustable'] = $videoEntry->isVideoEmbeddable();
        $return['privado'] = $videoEntry->isVideoPrivate();
        return $return;
    }

    function getApplet($jarFile, $className, $params = array(), $width = 1, $height = 1, $name = 'japplet') {
        $retVal = "";

        $useApplet = 0;
        $user_agent = $_SERVER['HTTP_USER_AGENT'];

        if (stristr($user_agent, "konqueror") || stristr($user_agent, "macintosh") || stristr($user_agent, "opera")) {
            $useApplet = 1;
            $retVal = sprintf('<applet name="%s" id="%s" archive="%s" code="%s" width="%s" height="%s" MAYSCRIPT >', $name, $name, $jarFile, $className, $width, $height);
        } else {
            if (strstr($user_agent, "MSIE")) {
                $retVal = sprintf('<object  name="%s" id="%s" classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" style="border-width:0;" codebase="http://java.sun.com/products/plugin/autodl/jinstall-1_4_1-windows-i586.cab#version=1,4,1"  width= "%s" height= "%s">', $name, $name, $width, $height);
            } else {
                $retVal = sprintf('<object  name="%s" id="%s" type="application/x-java-applet;version=1.4.1" width= "%s" height= "%s">', $name, $name, $width, $height);
            }

            $params['archive'] = $jarFile;
            $params['code'] = $className;
            $params['mayscript'] = 'true';
            $params['scriptable'] = 'true';
            $params['name'] = $name;
        }

        foreach ($params as $var => $val) {
            $retVal .= sprintf('<param name="%s" value="%s">', $var, $val);
        }

        $retVal .= 'Al parecer, usted no tiene Java instalado o está deshabilitado en su sistema.<br />
                    Por favor, descargue <a href="http://www.java.com/getjava/" class="link" target="_blank">Aqui</a>';
        if ($useApplet == 1) {
            $retVal .= '</applet>';
        } else {
            $retVal .= '</object>';
        }

        return $retVal;
    }

}

?>